import { getuuId } from './utils/uuid'


/**
 * Creates a new Node.
 * @class
 * @param{children } 子节点
 * @param{index} 1-2-3 : 1代表第一层第1个，2代表第二层第2个,3代表第三层第3个
 * @param{focus} 是否可编辑
 */
export default class Node {
  children: any;
  name: string;
  level: number;
  id: string;
  rootNode?: boolean;
  expanded: boolean;
  index: string;
  parentId: string;
  parentIndex: string;
  focus: boolean;
  className: string;
  customContent?: string;
  constructor(options: Record<string, any>) {
    this.children = [];
    this.index = options.index;
    this.level = options.level;
    this.name = options.name;
    this.rootNode = options.rootNode;
    this.id = options.id;
    this.expanded = true;
    this.parentId = options.parentId;
    this.parentIndex = options.parentIndex;
    this.focus = options.focus || false
    this.className = options.className
    this.customContent = options.customContent

    if (options.children && options.children.length > 0) {
      this.generateNodes(options.children, options.ids)
    }

  }

  generateNodes(data: Record<string, any>[], ids: string[]) {
    data.forEach((item, index) => {
      const nodeIndex = `${this.index}-${index + 1}`;
      const level = this.level + 1
      const node = new Node({
        ids: ids,
        name: item.label,
        children: item.children || [],
        id: getuuId(ids),
        index: nodeIndex,
        parentId: this.id,
        parentIndex: this.index,
        rootNode: false,
        level: level,
        customContent: item.customContent
      })
      this.children.push(node)
    });
  }


}

